package com.java110.mall.shop.bmo.storeInfo.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.store.StoreInfoDto;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.shop.IStoreInfoInnerServiceSMO;
import com.java110.mall.shop.bmo.storeInfo.IGetStoreInfoBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getStoreInfoBMOImpl")
public class GetStoreInfoBMOImpl implements IGetStoreInfoBMO {

    @Autowired
    private IStoreInfoInnerServiceSMO storeInfoInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     *
     *
     * @param  storeInfoDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(StoreInfoDto storeInfoDto) {


        int count = storeInfoInnerServiceSMOImpl.queryStoreInfosCount(storeInfoDto);

        List<StoreInfoDto> storeInfoDtos = null;
        if (count > 0) {
            storeInfoDtos = storeInfoInnerServiceSMOImpl.queryStoreInfos(storeInfoDto);
            freshIcons(storeInfoDtos);
        } else {
            storeInfoDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeInfoDto.getRow()), count, storeInfoDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     *
     *
     * @param  storeInfoDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> getStoreBySeq(StoreInfoDto storeInfoDto) {


        int count = storeInfoInnerServiceSMOImpl.queryStoreInfosCount(storeInfoDto);

        List<StoreInfoDto> storeInfoDtos = null;
        if (count > 0) {
            storeInfoDtos = storeInfoInnerServiceSMOImpl.queryStoreInfosBySeq(storeInfoDto);
            freshIcons(storeInfoDtos);
        } else {
            storeInfoDtos = new ArrayList<>();
        }
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeInfoDto.getRow()), count, storeInfoDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    private void freshIcons(List<StoreInfoDto> storeInfoDtos) {
        if (storeInfoDtos == null || storeInfoDtos.size() < 1) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (StoreInfoDto tmpProductDto : storeInfoDtos) {
            productIds.add(tmpProductDto.getStoreInfoId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_STORE_PIC);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (StoreInfoDto tmpDto : storeInfoDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpDto.getStoreInfoId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    url = imgUrl + tmpFileRelDto.getFileRealName();
                    tmpDto.setIcon(url);
                }
            }
        }
    }
}
